package com.base.cn.platform.os.controller.classes;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.classes.TeacherDataIsolation;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.classes.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 班级
 *
 * @author SK
 * @since 2018-05-08
 */
@Controller
@RequestMapping("/admin/classes")
public class AdminClassesController extends AdminBaseController {

    @Autowired
    private ClassesService classesService;

    /**
     * 初始化修改或添加班级页面
     *
     * @param id 班级页面id
     * @return 修改或添加班级页面
     */
    @RequestMapping({"/initUpdate", "initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "id", required = false) BigDecimal id,
                                  @RequestParam(value = "step", required = false, defaultValue = "1") int step) {
        Map<String, Object> classes = null;
        if (DataUtil.idIsNotNull(id)) {
            classes = classesService.findClassesById(id, step == 2, false, true, true, 3);
        }
        if (classes == null) {
            classes = Collections.emptyMap();
        }
        model.addAttribute("classes", classes);
        if (step == 2) {
            return resultPage("/classes/member/classes-member");
        }
        return resultPage("/classes/save-classes");
    }

    /**
     * 初始化班级列表页面
     *
     * @return 班级列表页面
     */
    @RequestMapping("/classesList")
    @TeacherDataIsolation(type=6)
    public String classesList(Model model) {
        return resultPage("/classes/classes-list");
    }

    /**
     * 班级列表页面
     *
     * @return 班级列表页面
     */
    @TeacherDataIsolation(type=1)
    @RequestMapping("/ajaxClassesListPage")
    public String ajaxClassesListPage(Model model,
                                      @RequestBody Map<String, Object> classes,
                                      @RequestParam("currentPage") int currentPage) {
        Map<String, Object> dataMap = classesService.findClassesListPage(classes, currentPage, null, true, true, true, true, 3);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/classes/ajax-classes-list");
    }

    /**
     * 初始化班级列表页面
     *
     * @return 班级列表页面
     */
    @RequestMapping("/selectClassesList")
    @TeacherDataIsolation(type=3)
    public String selectClassesList(Model model,
                                    @RequestParam(value = "courseIds", required = false) String courseIds,
                                    @RequestParam(value = "subjectIds",required = false) String subjectIds,
                                    @RequestParam(value = "type", required = false,defaultValue = "0") int type) {
        model.addAttribute("courseIds", courseIds);
        model.addAttribute("subjectIds", subjectIds);
        return resultPage("/classes/select-classes-list");
    }

    /**
     * 班级列表页面
     *
     * @return 班级列表页面
     */
    @RequestMapping("/ajaxSelectClassesListPage")
    public String ajaxSelectClassesListPage(Model model,
                                            @RequestBody Map<String, Object> classes,
                                            @RequestParam("currentPage") int currentPage) {
        classes.put("status",1);
        Map<String, Object> dataMap = classesService.findClassesListPage(classes, currentPage, null, true, true, true, true);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/classes/ajax-select-classes-list");
    }

    /**
     * 保存班级
     *
     * @param classes 班级
     * @return 保存结果
     */
    @RequestMapping("/saveClasses")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "新建班级")
    public Map<String, Object> saveClasses(HttpServletRequest request,
                                           @RequestBody Map<String, Object> classes) {
        BigDecimal createUserId = loginUtil.getLoginUserId(request);
        classes.put("createUserId", createUserId);
        return classesService.saveClasses(classes);
    }

    /**
     * 更新班级状态
     *
     * @param ids    班级id
     * @param status 班级状态 1.正常 2.停班
     * @return 操作结果
     */
    @RequestMapping("/updateClassesStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "更新班级状态")
    public Map<String, Object> updateClassesStatus(@RequestParam("ids") String ids,
                                                   @RequestParam("status") int status) {
        ids = DataUtil.filterNotNumberStr(ids);
        if (StringUtils.isEmpty(ids)) {
            return ResultUtil.ERROR("选择操作的班级");
        }
        return classesService.updateClassesStatus(ids, status);
    }

    /**
     * 更新班封面
     * @return 操作结果
     */
    @RequestMapping("/saveClassesImg")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "更新班级封面")
    public Map<String, Object> saveClassesImg(@RequestParam("id") BigDecimal id,
                                                   @RequestParam("imageJson") String imageJson) {
        if(StringUtils.isEmpty(imageJson)){
            return ResultUtil.ERROR("请选择图片");
        }
        Map<String,Object> map =new HashMap<>();
        map.put("id",id);
        map.put("imageJson",imageJson);
        return classesService.saveClassesImg(map);
    }



    /**
     * 初始化修改或添加班级页面
     *
     * @param id 班级页面id
     * @return 修改或添加班级页面
     */
    @RequestMapping("/editBrief")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "更新班级简介")
    public Map<String, Object>  editBrief(@RequestParam(value = "id") BigDecimal id,@RequestParam("summary") String summary) {
        HashMap<String, Object> classes = new HashMap<>();
        classes.put("id",id);
        classes.put("summary",summary);
        return   classesService.editBrief(classes);
    }
}
